package com.yyk.yiliao.ui.activity;

import android.annotation.SuppressLint;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseActivity;
import com.yyk.yiliao.base.BaseRecyclerAdapter;
import com.yyk.yiliao.base.BaseRecyclerHolder;
import com.yyk.yiliao.bean.City_Bean;
import com.yyk.yiliao.util.OkHttp3Utlis;
import com.yyk.yiliao.util.ebs.LocationEvent;
import com.yyk.yiliao.util.rx.ApiService;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * 定位 筛选
 */
public class Location_Activity extends BaseActivity {

    @BindView(R.id.mLocation_province)//省级
            TextView mLocationProvince;
    @BindView(R.id.mLocation_city)//市级
            RecyclerView mLocationCity;
    @BindView(R.id.mLocation_location)//提示定位信息
            TextView mLocationLocation;

    private BaseRecyclerAdapter<City_Bean.DataBean> adapter;

    private String province = "山东省";


    @Override
    public int getLayoutId() {
        return R.layout.activity_location;
    }

    @Override
    public void initView() {
        initToolbar();
    }

    private void initToolbar() {
        hidetoolBar();
    }


    @Override

    public void initData() {
        String locationTv = getIntent().getStringExtra("locationTv");
//        String locationTv = Hawk.get("locationTv");
        if (!TextUtils.isEmpty(locationTv)) {
            if (locationTv.equals("北京")) {
                mLocationLocation.setText(locationTv + "市暂无定可预约医院");

            } else {
                mLocationLocation.setText(locationTv + "市");

            }
        }

        mLocationProvince.setText(province);

        OkHttp3Utlis.getIntance().postOkhttp(ApiService.BASE_URL + "/mobile/common/city", null, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string().trim();
                Log.e("zzzz", result);
                Gson gson = new Gson();
                final City_Bean info = gson.fromJson(result, City_Bean.class);
                final List<City_Bean.DataBean> data = info.getData();
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        adapter = new BaseRecyclerAdapter<City_Bean.DataBean>(Location_Activity.this, data, R.layout.adapter_item_location) {
                            @Override
                            public void convert(BaseRecyclerHolder holder, City_Bean.DataBean item, int position, boolean isScrolling) {
                                holder.setText(R.id.nLocation_city, item.getAname());
                            }
                        };
                        mLocationCity.addItemDecoration(new DividerItemDecoration(Location_Activity.this, DividerItemDecoration.VERTICAL));
                        mLocationCity.setLayoutManager(new LinearLayoutManager(Location_Activity.this, LinearLayoutManager.VERTICAL, false));
                        mLocationCity.setAdapter(adapter);
                        adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
                            @SuppressLint("WrongConstant")
                            @Override
                            public void onItemClick(RecyclerView parent, View view, int position) {
                                String city_name = data.get(position).getAname();
                                String city_name1 = city_name.replace("市", "");
                                String aid = String.valueOf(data.get(position).getAid());
                                EventBus.getDefault().post(new LocationEvent(city_name1, aid + ""));
                                finish();


//                                Toast.makeText(Location_Activity.this, "点击了" + data.get(position) + "位置", 0).show();
                            }
                        });
                    }

                });
            }
        });
    }



    @OnClick({R.id.mLocation_city, R.id.mLocation_location})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.mLocation_city:

                break;
            case R.id.mLocation_location:
                break;
        }
    }
}
